<template>
	<view>
		<u-popup v-model="show" mode="center" border-radius="12">
			<view class="popVue padding40">
				<view class="flex align-center">
					<image
						src="https://aiet-file-center.oss-cn-hangzhou.aliyuncs.com/shuziren/audio/2024102816235961611.jpg"
						mode="widthFix" style="width: 46rpx;"></image>
					<text style="color: #333;font-weight: 600;padding-left: 20rpx;">香蕉数字人</text>
					<text style="color: #333;font-weight: 600;padding-left: 12rpx;">申请</text>
				</view>
				<view class="label">获取您的微信信息</view>
				<view class="btns flex-between align-center margin40">
					<view class="btn" @click="toggleShow()">拒绝</view>
					<view class="btn" @click="loginByWeChat()">允许</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
export default {
	name: 'WxLogin',
	props: {
		show: {
			type: Boolean,
			default: false
		},
		type: {
			type: Boolean,
			default: true
		}
	},
	methods: {
		toggleShow() {
			// 清除登录前存储路径
			uni.removeStorageSync("page");
			// 关闭弹窗
			this.$emit('update:show', !this.show);
		},
		// 微信授权登录
		loginByWeChat() {
			let that = this
			// 关闭弹窗
			this.$emit('update:show', !this.show);
			// 加载
			uni.showLoading()
			// 获取用户微信信息
			uni.login({
				provider: 'weixin',
				// #ifdef APP
				onlyAuthorize: true,
				// #endif
				success(loginRes) {
					// 获取code
					const { code } = loginRes;
					console.log(code)
					// 微信登录
					that.$request("/aiet-user-center/user/wxCodeLogin", { code }).then(res => {
						if (res.code === 200) {
							// 加载
							uni.hideLoading()
							// 登录成功提示
							that.$u.toast('登录成功');
							// 存储token
							uni.setStorageSync("ai-token", res.data.token)
							// 获取用户信息
							that.$store.dispatch("getUser")
							// 更改登录状态
							that.$store.state.isLogin = true
							  if (res.data.user.qrCode==="" || res.data.user.qrCode===null){
								that.drawQrCode();
							  }
							// 延迟跳转
							setTimeout(() => {
								// 校验手机号
								if (res.data.user.phone) {
									// 手机号存在, 查看是否有登录前存储路径
									const page = uni.getStorageSync("page")
									// 清除登录前存储路径
									uni.removeStorageSync("page")
									// 清除提示登录存储
									uni.removeStorageSync("login")
									// 存储存在跳转存储页面
									if (page.toString().length > 0) {
										uni.navigateTo({
											url: '/' + page
										})
									}
								} else {
									// 没有绑定手机号跳转绑定页面
									uni.navigateTo({
										url: "/pages/login/login"
									})
								}
							}, 1000)
						} else {
							uni.hideLoading()
							that.$u.toast(res.msg);
						}
					})
				},
				fail(loginErr) {
					uni.hideLoading()
					that.$u.toast(loginErr.errMsg);
					console.log("uni.login调用失败:", loginErr)
				}
			})
		},
    async drawQrCode() {
      const res = await this.$request("/aiet-user-center/user/getQRCode", {}, "GET")
      if (res.code === 200) console.log("获取小程序码",res)
    }
	}
}
</script>

<style lang="less" scoped>
.popVue {
	width: 650rpx;
	background: #fff;
	border-radius: 10rpx;

	.label {
		color: #000;
		font-weight: 600;
		padding: 50rpx 0 30rpx;
	}

	.btns {
		width: 560rpx;
		height: 80rpx;

		.btn {
			width: 260rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 10rpx;
			text-align: center;
			font-weight: 500;
		}

		.btn:nth-child(1) {
			background: #f5f5f5;
			color: #F8C329;
		}

		.btn:nth-child(2) {
			background: #F8C329;
			color: #fff;
		}
	}
}
</style>
